<!DOCTYPE html>












  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=6.5.0" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.5.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.5.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.5.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.5.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.5.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="问题我将我的Spring Cloud项目版本升到Finchley.M9后，集成了spring security oauth2的三件套,采用RedisTokenStore来存储token。项目正常启动后，请求token时报错。">
<meta name="keywords" content="Spring Boot 2.x中遇到的坑">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Security Oauth2 使用RedisTokenStore出错">
<meta property="og:url" content="https://lisb.gitee.io/2018/05/15/2018-05-15-spring security oauth2 使用redis存token时报错/index.html">
<meta property="og:site_name" content="码畜·小布">
<meta property="og:description" content="问题我将我的Spring Cloud项目版本升到Finchley.M9后，集成了spring security oauth2的三件套,采用RedisTokenStore来存储token。项目正常启动后，请求token时报错。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-08-22T04:22:13.797Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring Security Oauth2 使用RedisTokenStore出错">
<meta name="twitter:description" content="问题我将我的Spring Cloud项目版本升到Finchley.M9后，集成了spring security oauth2的三件套,采用RedisTokenStore来存储token。项目正常启动后，请求token时报错。">






  <link rel="canonical" href="https://lisb.gitee.io/2018/05/15/2018-05-15-spring security oauth2 使用redis存token时报错/">



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Spring Security Oauth2 使用RedisTokenStore出错 | 码畜·小布</title>
  











  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">码畜·小布</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lisb.gitee.io/2018/05/15/2018-05-15-spring security oauth2 使用redis存token时报错/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="小布">
      <meta itemprop="description" content="曾梦想仗剑走天涯，后来bug太多就没去">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="码畜·小布">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Spring Security Oauth2 使用RedisTokenStore出错
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-05-15 23:24:36" itemprop="dateCreated datePublished" datetime="2018-05-15T23:24:36+08:00">2018-05-15</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-08-22 12:22:13" itemprop="dateModified" datetime="2018-08-22T12:22:13+08:00">2018-08-22</time>
              
            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h1><p>我将我的Spring Cloud项目版本升到Finchley.M9后，集成了spring security oauth2的三件套,采用RedisTokenStore来存储token。项目正常启动后，请求token时报错。<br><a id="more"></a><br>贴出报错信息:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nested exception is java.lang.NoSuchMethodError: org.springframework.data.redis.connection.RedisConnection.set([B[B)V</span><br></pre></td></tr></table></figure></p>
<p>看到这个报错,我的第一反应就是版本出现了冲突，但是我的依赖的都是spring-boot-starter中的默认版本，然后在谷歌中找了一下，发现了这个：<br><a href="https://github.com/spring-projects/spring-security-oauth/issues/1230#event-1352041281" target="_blank" rel="noopener">https://github.com/spring-projects/spring-security-oauth/issues/1230#event-1352041281</a>。<br>果然是版本问题，spring-data-redis。然后我按照网页中的解决方法“spring-date-redis”改为2.0.1.RELEASE和”jedis“为2.9.0（显式声明），结果还是报一样的错，后来这个页面我又看了看，下面的人也是一堆同样的问题。怪我看issues没要仔细:joy::joy::joy:</p>
<h1 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h1><p>出现这个问题的原因主要是spring-data-redis 但是我不想降版本，所以我用了一个极端的方式解决的。spring-data-redis 2.0版本中set(String,String)被弃用了，要使用RedisConnection.stringCommands().set(…)，所有我自定义一个RedisTokenStore，代码和RedisTokenStore一样，只是把所有conn.set(…)都换成conn..stringCommands().set(…)，测试后方法可行。代码如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lishangbu.auth.core.config</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.connection.RedisConnection;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.connection.RedisConnectionFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.common.OAuth2AccessToken;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.common.OAuth2RefreshToken;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.provider.OAuth2Authentication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.provider.token.TokenStore;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.provider.token.store.redis.JdkSerializationStrategy;</span><br><span class="line"><span class="keyword">import</span> org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStoreSerializationStrategy;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * CustomizedRedisTokenStore</span></span><br><span class="line"><span class="comment"> * 暂时的折中方案，解决出现的</span></span><br><span class="line"><span class="comment"> * java.lang.NoSuchMethodError: org.springframework.data.redis.connection.RedisConnection.set([B[B)V</span></span><br><span class="line"><span class="comment"> * 异常</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lishangbu</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomizedRedisTokenStore</span> <span class="keyword">implements</span> <span class="title">TokenStore</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String ACCESS = <span class="string">"access:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String AUTH_TO_ACCESS = <span class="string">"auth_to_access:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String AUTH = <span class="string">"auth:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String REFRESH_AUTH = <span class="string">"refresh_auth:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String ACCESS_TO_REFRESH = <span class="string">"access_to_refresh:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String REFRESH = <span class="string">"refresh:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String REFRESH_TO_ACCESS = <span class="string">"refresh_to_access:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CLIENT_ID_TO_ACCESS = <span class="string">"client_id_to_access:"</span>;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String UNAME_TO_ACCESS = <span class="string">"uname_to_access:"</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">final</span> RedisConnectionFactory connectionFactory;</span><br><span class="line">	<span class="keyword">private</span> AuthenticationKeyGenerator authenticationKeyGenerator = <span class="keyword">new</span> DefaultAuthenticationKeyGenerator();</span><br><span class="line">	<span class="keyword">private</span> RedisTokenStoreSerializationStrategy serializationStrategy = <span class="keyword">new</span> JdkSerializationStrategy();</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> String prefix = <span class="string">""</span>;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">CustomizedRedisTokenStore</span><span class="params">(RedisConnectionFactory connectionFactory)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.connectionFactory = connectionFactory;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAuthenticationKeyGenerator</span><span class="params">(AuthenticationKeyGenerator authenticationKeyGenerator)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.authenticationKeyGenerator = authenticationKeyGenerator;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSerializationStrategy</span><span class="params">(RedisTokenStoreSerializationStrategy serializationStrategy)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.serializationStrategy = serializationStrategy;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPrefix</span><span class="params">(String prefix)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.prefix = prefix;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> RedisConnection <span class="title">getConnection</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> connectionFactory.getConnection();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">byte</span>[] serialize(Object object) &#123;</span><br><span class="line">		<span class="keyword">return</span> serializationStrategy.serialize(object);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">byte</span>[] serializeKey(String object) &#123;</span><br><span class="line">		<span class="keyword">return</span> serialize(prefix + object);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> OAuth2AccessToken <span class="title">deserializeAccessToken</span><span class="params">(<span class="keyword">byte</span>[] bytes)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> serializationStrategy.deserialize(bytes, OAuth2AccessToken.class);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> OAuth2Authentication <span class="title">deserializeAuthentication</span><span class="params">(<span class="keyword">byte</span>[] bytes)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> serializationStrategy.deserialize(bytes, OAuth2Authentication.class);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> OAuth2RefreshToken <span class="title">deserializeRefreshToken</span><span class="params">(<span class="keyword">byte</span>[] bytes)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> serializationStrategy.deserialize(bytes, OAuth2RefreshToken.class);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">byte</span>[] serialize(String string) &#123;</span><br><span class="line">		<span class="keyword">return</span> serializationStrategy.serialize(string);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> String <span class="title">deserializeString</span><span class="params">(<span class="keyword">byte</span>[] bytes)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> serializationStrategy.deserializeString(bytes);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OAuth2AccessToken <span class="title">getAccessToken</span><span class="params">(OAuth2Authentication authentication)</span> </span>&#123;</span><br><span class="line">		String key = authenticationKeyGenerator.extractKey(authentication);</span><br><span class="line">		<span class="keyword">byte</span>[] serializedKey = serializeKey(AUTH_TO_ACCESS + key);</span><br><span class="line">		<span class="keyword">byte</span>[] bytes = <span class="keyword">null</span>;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			bytes = conn.get(serializedKey);</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">		OAuth2AccessToken accessToken = deserializeAccessToken(bytes);</span><br><span class="line">		<span class="keyword">if</span> (accessToken != <span class="keyword">null</span></span><br><span class="line">				&amp;&amp; !key.equals(authenticationKeyGenerator.extractKey(readAuthentication(accessToken.getValue())))) &#123;</span><br><span class="line">			<span class="comment">// Keep the stores consistent (maybe the same user is</span></span><br><span class="line">			<span class="comment">// represented by this authentication but the details have</span></span><br><span class="line">			<span class="comment">// changed)</span></span><br><span class="line">			storeAccessToken(accessToken, authentication);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> accessToken;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OAuth2Authentication <span class="title">readAuthentication</span><span class="params">(OAuth2AccessToken token)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> readAuthentication(token.getValue());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OAuth2Authentication <span class="title">readAuthentication</span><span class="params">(String token)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] bytes = <span class="keyword">null</span>;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			bytes = conn.get(serializeKey(AUTH + token));</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">		OAuth2Authentication auth = deserializeAuthentication(bytes);</span><br><span class="line">		<span class="keyword">return</span> auth;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OAuth2Authentication <span class="title">readAuthenticationForRefreshToken</span><span class="params">(OAuth2RefreshToken token)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> readAuthenticationForRefreshToken(token.getValue());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OAuth2Authentication <span class="title">readAuthenticationForRefreshToken</span><span class="params">(String token)</span> </span>&#123;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="keyword">byte</span>[] bytes = conn.get(serializeKey(REFRESH_AUTH + token));</span><br><span class="line">			OAuth2Authentication auth = deserializeAuthentication(bytes);</span><br><span class="line">			<span class="keyword">return</span> auth;</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">storeAccessToken</span><span class="params">(OAuth2AccessToken token, OAuth2Authentication authentication)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] serializedAccessToken = serialize(token);</span><br><span class="line">		<span class="keyword">byte</span>[] serializedAuth = serialize(authentication);</span><br><span class="line">		<span class="keyword">byte</span>[] accessKey = serializeKey(ACCESS + token.getValue());</span><br><span class="line">		<span class="keyword">byte</span>[] authKey = serializeKey(AUTH + token.getValue());</span><br><span class="line">		<span class="keyword">byte</span>[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));</span><br><span class="line">		<span class="keyword">byte</span>[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));</span><br><span class="line">		<span class="keyword">byte</span>[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());</span><br><span class="line"></span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			conn.openPipeline();</span><br><span class="line">			conn.stringCommands().set(accessKey, serializedAccessToken);</span><br><span class="line">			conn.stringCommands().set(authKey, serializedAuth);</span><br><span class="line">			conn.stringCommands().set(authToAccessKey, serializedAccessToken);</span><br><span class="line">			<span class="keyword">if</span> (!authentication.isClientOnly()) &#123;</span><br><span class="line">				conn.rPush(approvalKey, serializedAccessToken);</span><br><span class="line">			&#125;</span><br><span class="line">			conn.rPush(clientId, serializedAccessToken);</span><br><span class="line">			<span class="keyword">if</span> (token.getExpiration() != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">int</span> seconds = token.getExpiresIn();</span><br><span class="line">				conn.expire(accessKey, seconds);</span><br><span class="line">				conn.expire(authKey, seconds);</span><br><span class="line">				conn.expire(authToAccessKey, seconds);</span><br><span class="line">				conn.expire(clientId, seconds);</span><br><span class="line">				conn.expire(approvalKey, seconds);</span><br><span class="line">			&#125;</span><br><span class="line">			OAuth2RefreshToken refreshToken = token.getRefreshToken();</span><br><span class="line">			<span class="keyword">if</span> (refreshToken != <span class="keyword">null</span> &amp;&amp; refreshToken.getValue() != <span class="keyword">null</span>) &#123;</span><br><span class="line">				<span class="keyword">byte</span>[] refresh = serialize(token.getRefreshToken().getValue());</span><br><span class="line">				<span class="keyword">byte</span>[] auth = serialize(token.getValue());</span><br><span class="line">				<span class="keyword">byte</span>[] refreshToAccessKey = serializeKey(REFRESH_TO_ACCESS + token.getRefreshToken().getValue());</span><br><span class="line">				conn.stringCommands().set(refreshToAccessKey, auth);</span><br><span class="line">				<span class="keyword">byte</span>[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + token.getValue());</span><br><span class="line">				conn.stringCommands().set(accessToRefreshKey, refresh);</span><br><span class="line">				<span class="keyword">if</span> (refreshToken <span class="keyword">instanceof</span> ExpiringOAuth2RefreshToken) &#123;</span><br><span class="line">					ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken) refreshToken;</span><br><span class="line">					Date expiration = expiringRefreshToken.getExpiration();</span><br><span class="line">					<span class="keyword">if</span> (expiration != <span class="keyword">null</span>) &#123;</span><br><span class="line">						<span class="keyword">int</span> seconds = Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / <span class="number">1000L</span>)</span><br><span class="line">								.intValue();</span><br><span class="line">						conn.expire(refreshToAccessKey, seconds);</span><br><span class="line">						conn.expire(accessToRefreshKey, seconds);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			conn.closePipeline();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">static</span> String <span class="title">getApprovalKey</span><span class="params">(OAuth2Authentication authentication)</span> </span>&#123;</span><br><span class="line">		String userName = authentication.getUserAuthentication() == <span class="keyword">null</span> ? <span class="string">""</span></span><br><span class="line">				: authentication.getUserAuthentication().getName();</span><br><span class="line">		<span class="keyword">return</span> getApprovalKey(authentication.getOAuth2Request().getClientId(), userName);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">static</span> String <span class="title">getApprovalKey</span><span class="params">(String clientId, String userName)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> clientId + (userName == <span class="keyword">null</span> ? <span class="string">""</span> : <span class="string">":"</span> + userName);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeAccessToken</span><span class="params">(OAuth2AccessToken accessToken)</span> </span>&#123;</span><br><span class="line">		removeAccessToken(accessToken.getValue());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OAuth2AccessToken <span class="title">readAccessToken</span><span class="params">(String tokenValue)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] key = serializeKey(ACCESS + tokenValue);</span><br><span class="line">		<span class="keyword">byte</span>[] bytes = <span class="keyword">null</span>;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			bytes = conn.get(key);</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">		OAuth2AccessToken accessToken = deserializeAccessToken(bytes);</span><br><span class="line">		<span class="keyword">return</span> accessToken;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeAccessToken</span><span class="params">(String tokenValue)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] accessKey = serializeKey(ACCESS + tokenValue);</span><br><span class="line">		<span class="keyword">byte</span>[] authKey = serializeKey(AUTH + tokenValue);</span><br><span class="line">		<span class="keyword">byte</span>[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue);</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			conn.openPipeline();</span><br><span class="line">			conn.get(accessKey);</span><br><span class="line">			conn.get(authKey);</span><br><span class="line">			conn.del(accessKey);</span><br><span class="line">			conn.del(accessToRefreshKey);</span><br><span class="line">			<span class="comment">// Don't remove the refresh token - it's up to the caller to do that</span></span><br><span class="line">			conn.del(authKey);</span><br><span class="line">			List&lt;Object&gt; results = conn.closePipeline();</span><br><span class="line">			<span class="keyword">byte</span>[] access = (<span class="keyword">byte</span>[]) results.get(<span class="number">0</span>);</span><br><span class="line">			<span class="keyword">byte</span>[] auth = (<span class="keyword">byte</span>[]) results.get(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">			OAuth2Authentication authentication = deserializeAuthentication(auth);</span><br><span class="line">			<span class="keyword">if</span> (authentication != <span class="keyword">null</span>) &#123;</span><br><span class="line">				String key = authenticationKeyGenerator.extractKey(authentication);</span><br><span class="line">				<span class="keyword">byte</span>[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + key);</span><br><span class="line">				<span class="keyword">byte</span>[] unameKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));</span><br><span class="line">				<span class="keyword">byte</span>[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());</span><br><span class="line">				conn.openPipeline();</span><br><span class="line">				conn.del(authToAccessKey);</span><br><span class="line">				conn.lRem(unameKey, <span class="number">1</span>, access);</span><br><span class="line">				conn.lRem(clientId, <span class="number">1</span>, access);</span><br><span class="line">				conn.del(serialize(ACCESS + key));</span><br><span class="line">				conn.closePipeline();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">storeRefreshToken</span><span class="params">(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] refreshKey = serializeKey(REFRESH + refreshToken.getValue());</span><br><span class="line">		<span class="keyword">byte</span>[] refreshAuthKey = serializeKey(REFRESH_AUTH + refreshToken.getValue());</span><br><span class="line">		<span class="keyword">byte</span>[] serializedRefreshToken = serialize(refreshToken);</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			conn.openPipeline();</span><br><span class="line">			conn.stringCommands().set(refreshKey, serializedRefreshToken);</span><br><span class="line">			conn.stringCommands().set(refreshAuthKey, serialize(authentication));</span><br><span class="line">			<span class="keyword">if</span> (refreshToken <span class="keyword">instanceof</span> ExpiringOAuth2RefreshToken) &#123;</span><br><span class="line">				ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken) refreshToken;</span><br><span class="line">				Date expiration = expiringRefreshToken.getExpiration();</span><br><span class="line">				<span class="keyword">if</span> (expiration != <span class="keyword">null</span>) &#123;</span><br><span class="line">					<span class="keyword">int</span> seconds = Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / <span class="number">1000L</span>)</span><br><span class="line">							.intValue();</span><br><span class="line">					conn.expire(refreshKey, seconds);</span><br><span class="line">					conn.expire(refreshAuthKey, seconds);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			conn.closePipeline();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OAuth2RefreshToken <span class="title">readRefreshToken</span><span class="params">(String tokenValue)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] key = serializeKey(REFRESH + tokenValue);</span><br><span class="line">		<span class="keyword">byte</span>[] bytes = <span class="keyword">null</span>;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			bytes = conn.get(key);</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">		OAuth2RefreshToken refreshToken = deserializeRefreshToken(bytes);</span><br><span class="line">		<span class="keyword">return</span> refreshToken;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeRefreshToken</span><span class="params">(OAuth2RefreshToken refreshToken)</span> </span>&#123;</span><br><span class="line">		removeRefreshToken(refreshToken.getValue());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeRefreshToken</span><span class="params">(String tokenValue)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] refreshKey = serializeKey(REFRESH + tokenValue);</span><br><span class="line">		<span class="keyword">byte</span>[] refreshAuthKey = serializeKey(REFRESH_AUTH + tokenValue);</span><br><span class="line">		<span class="keyword">byte</span>[] refresh2AccessKey = serializeKey(REFRESH_TO_ACCESS + tokenValue);</span><br><span class="line">		<span class="keyword">byte</span>[] access2RefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue);</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			conn.openPipeline();</span><br><span class="line">			conn.del(refreshKey);</span><br><span class="line">			conn.del(refreshAuthKey);</span><br><span class="line">			conn.del(refresh2AccessKey);</span><br><span class="line">			conn.del(access2RefreshKey);</span><br><span class="line">			conn.closePipeline();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeAccessTokenUsingRefreshToken</span><span class="params">(OAuth2RefreshToken refreshToken)</span> </span>&#123;</span><br><span class="line">		removeAccessTokenUsingRefreshToken(refreshToken.getValue());</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">removeAccessTokenUsingRefreshToken</span><span class="params">(String refreshToken)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] key = serializeKey(REFRESH_TO_ACCESS + refreshToken);</span><br><span class="line">		List&lt;Object&gt; results = <span class="keyword">null</span>;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			conn.openPipeline();</span><br><span class="line">			conn.get(key);</span><br><span class="line">			conn.del(key);</span><br><span class="line">			results = conn.closePipeline();</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (results == <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">byte</span>[] bytes = (<span class="keyword">byte</span>[]) results.get(<span class="number">0</span>);</span><br><span class="line">		String accessToken = deserializeString(bytes);</span><br><span class="line">		<span class="keyword">if</span> (accessToken != <span class="keyword">null</span>) &#123;</span><br><span class="line">			removeAccessToken(accessToken);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Collection&lt;OAuth2AccessToken&gt; <span class="title">findTokensByClientIdAndUserName</span><span class="params">(String clientId, String userName)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(clientId, userName));</span><br><span class="line">		List&lt;<span class="keyword">byte</span>[]&gt; byteList = <span class="keyword">null</span>;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			byteList = conn.lRange(approvalKey, <span class="number">0</span>, -<span class="number">1</span>);</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (byteList == <span class="keyword">null</span> || byteList.size() == <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span> Collections.&lt;OAuth2AccessToken&gt; emptySet();</span><br><span class="line">		&#125;</span><br><span class="line">		List&lt;OAuth2AccessToken&gt; accessTokens = <span class="keyword">new</span> ArrayList&lt;OAuth2AccessToken&gt;(byteList.size());</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">byte</span>[] bytes : byteList) &#123;</span><br><span class="line">			OAuth2AccessToken accessToken = deserializeAccessToken(bytes);</span><br><span class="line">			accessTokens.add(accessToken);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> Collections.&lt;OAuth2AccessToken&gt; unmodifiableCollection(accessTokens);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Collection&lt;OAuth2AccessToken&gt; <span class="title">findTokensByClientId</span><span class="params">(String clientId)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">byte</span>[] key = serializeKey(CLIENT_ID_TO_ACCESS + clientId);</span><br><span class="line">		List&lt;<span class="keyword">byte</span>[]&gt; byteList = <span class="keyword">null</span>;</span><br><span class="line">		RedisConnection conn = getConnection();</span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			byteList = conn.lRange(key, <span class="number">0</span>, -<span class="number">1</span>);</span><br><span class="line">		&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">			conn.close();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (byteList == <span class="keyword">null</span> || byteList.size() == <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span> Collections.&lt;OAuth2AccessToken&gt; emptySet();</span><br><span class="line">		&#125;</span><br><span class="line">		List&lt;OAuth2AccessToken&gt; accessTokens = <span class="keyword">new</span> ArrayList&lt;OAuth2AccessToken&gt;(byteList.size());</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">byte</span>[] bytes : byteList) &#123;</span><br><span class="line">			OAuth2AccessToken accessToken = deserializeAccessToken(bytes);</span><br><span class="line">			accessTokens.add(accessToken);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> Collections.&lt;OAuth2AccessToken&gt; unmodifiableCollection(accessTokens);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>至此问题就当解决:joy::joy::joy:</p>
<h1 id="更优雅的解决方案"><a href="#更优雅的解决方案" class="headerlink" title="更优雅的解决方案"></a>更优雅的解决方案</h1><p>会出现这个问题主要是因为Finchley.M9的spring-security-oauth2依赖的redis版本过低，升级到2.3.3版本就可以解决这个问题。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-security<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--排除有问题的旧版本redis操作--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-security-oauth2<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.security.oauth<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.security.oauth<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-security-oauth2<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3.3.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure></p>

      
    </div>

    

    
    
    

    

    
       
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Spring-Boot-2-x中遇到的坑/" rel="tag"># Spring Boot 2.x中遇到的坑</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/05/05/2018-05-05-个性化用户认证流程(一)/" rel="next" title="个性化用户认证流程(一)">
                <i class="fa fa-chevron-left"></i> 个性化用户认证流程(一)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">小布</p>
              <p class="site-description motion-element" itemprop="description">曾梦想仗剑走天涯，后来bug太多就没去</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">5</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">3</span>
                    <span class="site-state-item-name">标签</span>
                  
                </div>
              
            </nav>
          

          

          

          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#问题"><span class="nav-number">1.</span> <span class="nav-text">问题</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#解决方案"><span class="nav-number">2.</span> <span class="nav-text">解决方案</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#更优雅的解决方案"><span class="nav-number">3.</span> <span class="nav-text">更优雅的解决方案</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">小布</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v6.5.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.5.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.5.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.5.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.5.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.5.0"></script>



  



  










  





  

  

  

  

  

  
  

  

  

  

  

  

  

</body>
</html>
